home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 1114 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  2.2 KB

  1. Path: fido.asd.sgi.com!austern
  2. From: Igor Boukanov <Igor.Boukanov@fi.uib.no>
  3. Newsgroups: comp.std.c++
  4. Subject: Just one more library function
  5. Date: 16 Apr 1996 13:15:25 PDT
  6. Organization: Fysisk institutt, Universitetet i Bergen
  7. Approved: austern@isolde.mti.sgi.com
  8. Message-ID: <Pine.HPP.3.91.960416133333.19297A-100000@hadron.fi.uib.no>
  9. NNTP-Posting-Host: isolde.mti.sgi.com
  10. Keywords: C++, array allocation
  11. X-Original-Date: Tue, 16 Apr 1996 13:35:30 +0200 (METDST)
  12. X-Auth: PGPMoose V1.1 PGP comp.std.c++
  13.     iQBVAwUBMXP/3ky4NqrwXLNJAQF1PQH+Oz8hPB4xL86CtVrQAvSggZZY/+pWBVF3
  14.     JgXh4w7izuyrKvDRJM76LJn6hKIc/h+kOm+B0UgciwKaPruzIGXUkg==
  15.     =smCf
  16. Originator: austern@isolde.mti.sgi.com
  17.  
  18.     Why do not require that every C++ implementation should provide function 
  19. that will return for the pointer obtained from new[] the number of 
  20. allocated elements, i.e. something like:
  21.  
  22. template<class T> size_t array_allocation_size(T* p);
  23.  
  24. So for 'int* p = new int[3];' 'array_allocation_size(p)' will return 3.
  25.  
  26. Such function will be very easy to implement because in the case of
  27. delete[] compiler use something like this. So why do not make it available 
  28. for programmer?
  29.  
  30. In this case it will be possible to emulate placement delete for arrays, i.e.
  31. one can write:
  32.  
  33. template<class T> void array_placement_delete(T* p) {
  34.    size_t i = array_allocation_size(p);
  35.    try { //attempt to support throw in destructors
  36.       while (i > 0) {
  37.          --i;
  38.          p[i].T::~T();
  39.       }
  40.    }
  41.    catch(...) {
  42.       try {
  43.          while(i > 0) {
  44.             --i;
  45.             p[i].T::~T();
  46.          }
  47.       catch(...) {
  48.          terminate();
  49.       }
  50.       throw();
  51.    }
  52. }
  53.  
  54. and array size doesn't need to be passed to array_placement_delete like in 
  55. the ordinary delete[] case...
  56.  
  57. So what do you think?
  58.  
  59. --
  60. Regards, Igor Boukanov. 
  61. igor.boukanov@fi.uib.no  
  62. http://www.fi.uib.no/~boukanov/
  63. ---
  64. [ comp.std.c++ is moderated.  To submit articles: Try just posting with your 
  65.                 newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  66.   comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  67.   Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  68.   Comments? mailto:std-c++-request@ncar.ucar.edu 
  69. ]
  70.